package daily.jun0617;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JPopupMenuTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShow());
    }

    public static void createAndShow() {
        //1.创建窗体，设置大小，位置等属性
        JFrame jFrame = new JFrame("单选框按钮");
        jFrame.setBounds(300, 400, 400, 300);
        jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        //2.设置窗体布局
        jFrame.setLayout(new BorderLayout());

        //3.创建弹出式菜单对象
        JPopupMenu popupMenu = new JPopupMenu();
        //4.创建两个菜单项
        JMenuItem item1 = new JMenuItem("查看");
        JMenuItem item2 = new JMenuItem("刷新");
        //5.将菜单项添加到弹出式菜单
        popupMenu.add(item1);
        popupMenu.addSeparator();
        popupMenu.add(item2);

        //6.为窗体注册鼠标事件监听器
        jFrame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(MouseEvent.BUTTON3 == e.getButton()){
                    popupMenu.show(e.getComponent(),e.getX(),e.getY());
                }
            }
        });
    }
}
